<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :edit="edit" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		changeEditor, 
		configSelectData,
		getEditor
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		ptFloat,
		minusF,
		getDateStr,
		errorModal
	} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				initValue:{
					newProductId:''
				},
				newProductCacheKey:'aft-bx-repairReportForm-title5-newProductCacheKey',
				repairReportTime:'',
				title:'',
				exists:[],
				unitDecimalPlaces:0,
				instCache:'aft-bx-repairReportForm-inst-newProductCacheKey',
				unitCache:'aft-bx-repairReportForm-inst-unitCacheKey',
				expectFinishDate:'',
			})
		},
		methods:{
			onInputWarrantyNum(val){
				let _that = this
				_that.$refs.autoform.formdata.warrantyNum = Number(val).toFixed(_that.unitDecimalPlaces)
			},
			clearNewProductCode(){
				let _that = this
				_that.$refs.autoform.clear("title1",'newProductId')
				_that.$refs.autoform.clear("title1",'newProductName')
				_that.$refs.autoform.clear("title1",'customerProductName')
				_that.$refs.autoform.clear("title1",'newProductStd')
				_that.$refs.autoform.clear("title1",'productCategoryName')
				_that.$refs.autoform.clear("title1",'customerProductStd')
				_that.$refs.autoform.clear("title1",'unitCode')
				_that.$refs.autoform.clear("title1",'productCategoryName')
				_that.$refs.autoform.clear("title1",'marketingPlanningProductId')
				_that.$refs.autoform.clear("title1",'warrantyNum')
				_that.$refs.autoform.clear("title1",'askDesperation')
				_that.$refs.autoform.clear("title1",'expectFinishDate')
				_that.$refs.autoform.clear("title1",'productionDate')
				_that.$refs.autoform.clear("title1",'isGuaranteeId')
				_that.$refs.autoform.clear("title1",'guaranteeDate')
				_that.$refs.autoform.clear("title1",'instId')
				_that.$refs.autoform.clear("title1",'remark')
				_that.$refs.autoform.clear("title1",'repairTaskNumber')
				_that.$refs.autoform.clear("title1",'afterApprovalNumber')
				_that.$refs.autoform.clear("title1",'afterSaveNumber')
			},
			onSelectGuarantee(val){
				let _that = this
				if(_that.$refs.autoform.formdata.productionDate){
					let title1 = _that.$refs.autoform.getEditorTitle("title1","productionDate")
					let title2 = _that.$refs.autoform.getEditorTitle("title1","guaranteeDate")
					if(moment(val).isBefore(_that.$refs.autoform.formdata.productionDate)){
						errorModal(_that,'提示信息',title2+"不能小于"+title1,()=>{
							_that.$refs.autoform.clear("title1","guaranteeDate")
						})
					}
				}
			},
			onSelectProduction(val){
				let _that = this
				let time = moment(_that.repairReportTime).format('YYYY-MM-DD')
				let title1 = _that.$refs.autoform.getEditorTitle("title1","productionDate")
				let title2 = _that.$refs.autoform.getEditorTitle("title1","guaranteeDate")
				if(_that.$refs.autoform.formdata.guaranteeDate){
					if(moment(val).isAfter(_that.$refs.autoform.formdata.guaranteeDate)){
						errorModal(_that,'提示信息',title2+"不能大于"+title1,()=>{
							_that.$refs.autoform.clear("title1","productionDate")
						})
					}
				}
				if(moment(val).isAfter(time)){
					errorModal(_that,'提示信息',title1+"不能大于"+_that.title,()=>{
						_that.$refs.autoform.clear("title1","productionDate")
					})
				}
			},
			watchInput(val){
				let _that = this
				let title = _that.$refs.autoform.getEditorTitle("title1","repairNum")
				if(!val){
					errorModal(_that,"提示信息",title + "不小于0",()=>{
						_that.$refs.autoform.clear("title1","repairNum")
					})
				}
			},
			initload(){
				let _that = this
				let arr = [
					_that.om.request(
						"dictSource/loadCombo",
						{
							sourceId:'283',
							categoryId:'6431'
						}
					),
					_that.om.request(
						"tsysdict/getCombobox",
						{
							code:'39053'
						}
					)
					// _that.om.request(
					// 	"inventoryCategory/getExistingProductLineAssortment",
					// 	{}
					// )
				]
				Promise.all(arr).then(res=>{
					_that.$refs.autoform.changeEditor("title1","isGuaranteeId","data",configSelectData(res[0],'id','text'))
					_that.$refs.autoform.initSelectIndex("title1","isGuaranteeId")
					_that.$refs.autoform.changeEditor("title1","marketingPlanningProductId","data",configSelectData(res[1],'id','dicname'))
					_that.$refs.autoform.initSelectIndex("title1","marketingPlanningProductId")
					_that.$nextTick(function(){
						if(!_that.index){
							_that.$refs.autoform.formdata.expectFinishDate = _that.expectFinishDate							
						}
					})
				})
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				_that.exists = _that.datas.filter((e,i)=>{
					return (i != _that.index)
				}).map(e=>e.newProductId).join(",")
				if(e.repairReportTime){
					_that.repairReportTime = e.repairReportTime
				}
				if(e.title){
					_that.title = e.title
				}
				if(e.expectFinishDate){
					_that.expectFinishDate = e.expectFinishDate
				}
				
				if (e.configDataCacheKey) {
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if (res.data && res.data instanceof Array) {
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
							}
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							changeEditor('title1', "newProductId", {
								'link': '../../public/pageSearch/pageSearch',
								"linkParam": {
									other:JSON.stringify({
										ids: _that.exists,
										likeFlag: false
									}),
									url:'productShowSecond/getProductPage',
									namefield: 'projectCode',
									extrafield:'projectStd',
									notefield:'projectName',
									searchfield: "q",
									initload:true,
									cachename: _that.newProductCacheKey,
								}
							}, null, _that.autoformdata)
							changeEditor("title1","repairNum","watchInput","watchInput",_that.autoformdata)
							changeEditor("title1","repairNum","precision",'0',_that.autoformdata)
							changeEditor("title1","instId",{
								link:'../../public/search/search',
								"linkParam":{
									other:JSON.stringify({}),
									url:'tinstitution/getComboTree',
									namefield:'text',
									initload:true,
									cachename:_that.instCache
								}
							},null,_that.autoformdata)
							changeEditor("title1","unitCode",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({}),
									url:'uomGroup/selectAllChildGrid',
									namefield:'unitName',
									extrafield:'unitCode',
									notefield:'unitGroupName',
									searchfield:'q',
									initload:true,
									cachename:_that.unitCache
								}
							},null,_that.autoformdata)
							
							let editor = getEditor("title1","warrantyNum",_that.autoformdata)
							if(_that.formdata[editor.precisionField] + ''){
								_that.unitDecimalPlaces = _that.formdata[editor.precisionField]
							}
							
							changeEditor(_that.autoformdata[0].editors, "fileList", "fileSaveNameField", "fileSaveName")
							changeEditor(_that.autoformdata[0].editors, "fileList", "uploadUrl", "uploadFileData/uploadInterimFile")
							changeEditor(_that.autoformdata[0].editors, "fileList", "loadUrl", "uploadFileData/getInterimFile")
							changeEditor(_that.autoformdata[0].editors, "fileList", "fileUploadKey", 154)
							if(_that.formdata.fileList) {
								var tempFileList = []
								var fileUrlList = _that.formdata.fileList.map(t=>{
									tempFileList.push(t.fileSaveName)
									return t.fileSaveName
								})
								changeEditor(_that.autoformdata[0].editors, "fileList", "fileUrlList", fileUrlList)
								changeEditor(_that.autoformdata[0].editors, "fileList", "tempFileList", tempFileList)
							}
							_that.initload()
						}
					})
				}
			})
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key:_that.newProductCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.newProductCode = d.newProductCode
							formdata.newProductName = d.newProductName
							formdata.newProductStd = d.newProductStd
							formdata.productCategoryName = d.productCategoryName
							formdata.customerProductName = d.newProductName
							formdata.customerProductStd = d.newProductStd
							formdata.newProductId = d.id
							formdata.unitCode = d.unitCode
							formdata.unitName = d.unitName
							_that.unitDecimalPlaces = d.unitDecimalPlaces
							let editor = _that.$refs.autoform.getEditor("title1","warrantyNum")
							_that.$refs.autoform.formdata[editor.precisionField] = d.unitDecimalPlaces
							uni.removeStorage({
								key:_that.newProductCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.instCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.instId = d.id
							formdata.instName = d.text
							uni.removeStorage({
								key:_that.instCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.unitCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.unitCode = d.unitCode
							formdata.unitName = d.unitName
							uni.removeStorage({
								key:_that.unitCache
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
